package com.gsmdata;

import com.utils.GsmDataCreateUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

/**
 * @author Administrator
 */
public class GsmDataRecordReader extends RecordReader<NullWritable, Text> {

    private int current = 0;
    private int number = 0;
    private Text valueText = new Text();

    @Override
    public void initialize(InputSplit split, TaskAttemptContext context) {
        this.number = ((GsmDataInputSplit) split).getNumber();
    }


    @Override
    public boolean nextKeyValue() {
        if (current++ < number) {
            valueText.set(GsmDataCreateUtils.getGsmRecord(current));
            return true;
        }
        return false;
    }

    @Override
    public NullWritable getCurrentKey() {
        return NullWritable.get();
    }

    @Override
    public Text getCurrentValue() {
        return valueText;
    }

    @Override
    public float getProgress() {
        return current * 1.0f / number;
    }

    @Override
    public void close() {
    }
}
